

#include "Model.h"
#include <iostream>
#include <QMouseEvent>
#include "NodeTop.h"
#include "DumpObjectTree.h"

using namespace std;

Model::Model(QWidget * parent)
: QFrame(parent) {
    controller = new Controller();
    setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
    setObjectName("model");
    setFrameStyle(QFrame::Panel | QFrame::Sunken);
}

void Model::mousePressEvent(QMouseEvent* event) {

    // get the equation input line edit
    QLineEdit* le = parent()->findChild<QLineEdit*>("equationInputLine");
    Q_ASSERT_X(le != 0, "", "Unable to find equationInputLine.");

    // If there is something in it parse it.
    //    if (not le->text().isEmpty()) {

    Node* n = controller->parse(le->text());
    Q_ASSERT_X(n != 0 and n != nullptr, "",
            "Input was not successfully parsed.");
    Q_ASSERT_X(n->getNodeType() != "NodeTop", "",
            "Cannot place a top level node in a top level node.");
    new NodeTop(event->pos(), this, n);
    le->clear();

    //    } else { // else just make an new empty tree
    //        new NodeTop(event->pos(), this);
    //    }


    DumpTree::dumpNodeTree(this);
//    DumpTree::dumpObjectTree(this);
    std::cout << std::endl;

    event->accept();
}

QSize Model::sizeHint() const {
    return QSize(600, 400);
}